package main.java.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;

public class Server {
    private ServerSocket serverSocket;
    private Set<Socket> cilents = new HashSet<>();

    public Server(){
        try {
            System.out.println("服务器启动中");
            serverSocket = new ServerSocket(8088);
            System.out.println("服务器启动成功");
        } catch (IOException e) {
            System.out.println("服务器启动失败");
            e.printStackTrace();
        }

    }
    public void start(){
        try {
            while (true) {
                System.out.println("等待客户端连接");
                Socket socket = serverSocket.accept();
                System.out.println("客户端连接成功");
                cilents.add(socket); // 新增：将新连接的客户端加入集合
                ClientHandler clientHandler = new ClientHandler(socket);
                Thread t = new Thread(clientHandler);
                t.start();
            }
        } catch (IOException e) {
            System.out.println("客户端连接失败");
            e.printStackTrace();
        }finally {
            try {
                serverSocket.close();
                System.out.println("服务器关闭");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    class ClientHandler implements Runnable{
        private Socket socket;
        public ClientHandler(Socket socket){
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                InputStream inputStream = socket.getInputStream();
                int len;
                while ((len = inputStream.read()) != -1) {
                    byte[] data = new byte[len];
                    inputStream.read(data);
                    String message = new String(data, StandardCharsets.UTF_8);
                    if ("exit".equals(message)) {
                        cilents.remove(socket);
                        break;
                    }
                    sendMsg(message);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    socket.close();
                    System.out.println("客户端退出");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        public void sendMsg(String msg){
            try {
                for (Socket cilent: cilents){
                    if (cilent!=socket){
                        OutputStream outCilent = cilent.getOutputStream();
                        outCilent.write(msg.getBytes(StandardCharsets.UTF_8));
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }
}
